package com.cheng.xbjy.service.impl;

import com.cheng.xbjy.dao.DeptDao;
import com.cheng.xbjy.dao.MeetingDao;
import com.cheng.xbjy.entity.Dept;
import com.cheng.xbjy.entity.Meeting;
import com.cheng.xbjy.entity.vo.PageResult;
import com.cheng.xbjy.entity.vo.Result;
import com.cheng.xbjy.service.MeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class MeetingServiceImpl implements MeetingService {

    @Autowired
    private MeetingDao meetingDao;

    @Autowired
    private DeptDao deptDao;

    @Override
    public Page<Meeting> findPage(String title, Long status, Integer page) {
        if (status == null) {
            return meetingDao.findByTitleLike("%" + title + "%"
                    , PageRequest.of(page - 1, PageResult.PAGE_SIZE));
        }
        return meetingDao.findByTitleLikeAndStatus("%" + title + "%", status
                , PageRequest.of(page - 1, PageResult.PAGE_SIZE));
    }

    @Override
    public Map<String, Object> findDetail(Long meetingId, Long userId) {
        Map<String, Object> map = new HashMap<>();
        Meeting meeting = meetingDao.findById(meetingId).get();
        //是否参加会议
        Integer isJoin = meetingDao.findByMidAndUid(meetingId, userId);

        //应道人数
        Integer makeUserCount = meeting.getMakeUser().split(",").length;

        //实到人数
        Integer joinCount = meetingDao.countByMid(meetingId);

        //未到人数
        Integer notJoinCount = makeUserCount - joinCount;

        map.put("makeUserCount", makeUserCount);
        map.put("isJoin", isJoin);
        map.put("meeting", meeting);
        map.put("joinCount", joinCount);
        map.put("notJoinCount", notJoinCount);
        return map;
    }

    @Transactional
    @Override
    public Result joinMeeting(Long meetingId, Long userId) {
        Meeting meeting = meetingDao.findById(meetingId).get();

        //是否已经参加会议
        Integer isJoin = meetingDao.findByMidAndUid(meetingId, userId);
        if (isJoin > 0) {
            meetingDao.deleteByMidAndUid(meetingId, userId);
            return new Result(true, "会议取消成功");
        }

        if (meeting.getStatus() == 1L) {
            return new Result(false, "会议已开始");
        }
        if (meeting.getStatus() == 2L) {
            return new Result(false, "会议已结束");
        }
        //查看你是否在抄送人中
        List<String> makeUserList = Arrays.asList(meeting.getMakeUser().split(","));
        if (!makeUserList.contains(userId.toString())) {
            return new Result(false, "您不可以加入此会议");
        }

        meetingDao.insertByMidAndUid(meetingId, userId);
        return new Result(true, "加入会议成功");
    }

    @Override
    public void save(Meeting meeting) {
        meeting.setPublishDate(new Date());

        Dept dept = deptDao.findById(meeting.getDeptId()).get();
        meeting.setDeptName(dept.getName());

        meeting.setStatus(0L);

        meetingDao.save(meeting);
    }

    @Override
    public List<Meeting> findByStatusNot(Long status) {
        return meetingDao.findByStatusNot(status);
    }

    @Transactional
    @Override
    public void updateStatus(Long id, Long status) {
        meetingDao.updateStatus(id, status);
    }
}
